<?php
class AdminController extends Zend_Controller_Action {
	public function preDispatch() { 
		$auth = Zend_Auth::getInstance(); 
		if (!$auth->hasIdentity()) { 
			$this->_redirect('login'); 
		}
		else {
			if(!NerdPass_Acl::isAllowed($auth->getIdentity(), 'usermanager')) {
				$this->_forward('denied', 'error');
			}
		}
	}
	
	public function indexAction() {
		
	}
	
	public function addAction() {
		if (count($_POST) <= 0) {
			$this->_redirect('admin');
		}
		
		// self::addUser('admin', 'letmein', '1834', '5pgoeSqcxvT+lJzkfoVonB7V7U8=', 'hurcdvjbrkbibbedgleetjuntdecebhnktkbcvdgdkbh', 'admin')
		
		/* Validate form */
		$validators = array(
			'username' => 'Alnum',
			'otp' => array(
				'Alnum',
				new Zend_Validate_StringLength(13, 128),
			),
			'password' => array('presence' => 'required'),
			'role' => array('presence' => 'required'),
			'apiid' => 'Digits',
			'sigkey' => array('presence' => 'required'),
			
		);
		$input = new Zend_Filter_Input(NULL, $validators, $_POST);
		if ($input->hasInvalid() || $input->hasMissing()) {
			$this->view->message = $input->getMessages();
		}
		else {
			NerdPass_Data::addUser($_POST['username'], $_POST['password'], $_POST['apiid'], $_POST['sigkey'], $_POST['otp'], $_POST['role']);
			$this->view->message = "User has been added successfully.";
		}
		
		if (!isset($_SERVER['HTTP_REFERER'])) {
			$refurl = '/';
		}
		else {
			$refurl = $_SERVER['HTTP_REFERER'];
		}
		$this->view->returnUrl = $refurl;
	}
}